Tokenizer: assign a parenthesis_owner for anonymous classes with parentheses#2595
Merged
gsherwood merged 1 commit intosquizlabs:masterfrom Sep 5, 2019
Conversation
…nthesis
Code:
```php
$anonClass = new class($arg) {};
```
As anonymous classes have a distinct token, it makes sense to me to assign that token as the parenthesis owner, along the same lines as is done for anonymous functions.
The main difference is that for anonymous classes, the parenthesis are optional.
Assigning an owner for them can not be done from within the `Tokenizer::createTokenMap()` as at that point in time the `PHP::processAdditional()` method hasn't run yet, so the token is still a `T_CLASS`.
It can however be adjusted from within the `PHP::processAdditional()` method when the `T_CLASS` token is changed to a `T_ANON_CLASS` token.
This commit implements this.
This means that for the `class` token in the above code snippet will now have a `parenthesis_owner`, `parenthesis_opener` and `parenthesis_closer` in the `$tokens` array.
The parenthesis opener and closer will both now also have the `parenthesis_owner` key, in addition to the `parenthesis_opener` and `parenthesis_closer` keys which they already had.
Instead of testing this via existing sniffs, I have chosen to add a new set of `Core` tests for specific tokenizer issues, with the tests for this change being the first set of tests added.
Includes:
* Adding the `T_ANON_CLASS` token to the `Tokens::$parenthesisOpeners` array.
* Removing the `T_ANON_CLASS` from the "additional tokens indicating that parenthesis are not arbitrary" list in the `Generic.WhiteSpace.ArbitraryParenthesesSpacing` sniff.
Member
|
Thanks a lot for this. I was just about to need it for the PSR-12 standard. I ended up moving the unit tests to remove the PHP folder because I'm committed to removing the other tokenizers and wanted to leave the core PHP tests in the main folder. I'd probably add a sub-folder for Commenting if that remains as-is. But thanks a lot for those first tokenizer tests as well. |
gsherwood
added a commit
that referenced
this pull request
Sep 5, 2019
Contributor
Author
|
There was a reason I made the change, just couldn't pull it earlier as it would conflict with and benefit from the change in #2295, so that needed to be merged first. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Code:
As anonymous classes have a distinct token, it makes sense to me to assign that token as the parenthesis owner, along the same lines as is done for anonymous functions.
The main difference is that for anonymous classes, the parenthesis are optional.
Assigning an owner for them can not be done from within the
Tokenizer::createTokenMap()as at that point in time thePHP::processAdditional()method hasn't run yet, so the token is still aT_CLASS.It can however be adjusted from within the
PHP::processAdditional()method when theT_CLASStoken is changed to aT_ANON_CLASStoken.This commit implements this.
This means that for the
classtoken in the above code snippet will now have aparenthesis_owner,parenthesis_openerandparenthesis_closerin the$tokensarray.The parenthesis opener and closer will both now also have the
parenthesis_ownerkey, in addition to theparenthesis_openerandparenthesis_closerkeys which they already had.Instead of testing this via existing sniffs, I have chosen to add a new set of
Coretests for specific tokenizer issues, with the tests for this change being the first set of tests added.Includes:
T_ANON_CLASStoken to theTokens::$parenthesisOpenersarray.T_ANON_CLASSfrom the "additional tokens indicating that parenthesis are not arbitrary" list in theGeneric.WhiteSpace.ArbitraryParenthesesSpacingsniff.